<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/8
 * Time: 13:45
 * 审核管理
 */
class Check extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('check_model');
        $this->load->model('procurement_model');
        $this->load->model('returnOrder_model');
        $this->load->model('purchaseOrder_model');


        $this->load->model('goldCheck_model');
        $this->load->model('biddingcheck_model');

        $this->load->model('biddingconfirm_model');

        $this->load->model('stockOut_model');
        $this->load->model('stockPut_model');



    }

    // 审核首页
    public function index()
    {

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        //没有搜索的情况下
        $_GET['type'] = isset($_GET['type']) ? $_GET['type'] : 1;
        if ($_GET['type'] == 1) {
            $checkWhere = [
                'type' => 1,
                //更换成动态名称
                'checker' =>  $this->session->userdata('nickname')
//                    'checker' => '张总'
            ];

//                echo '<pre>';
//                print_r($checkWhere);
//print_r($_COOKIE['nickname']);
//die();
            //查询审核的数据
            //刚进来为待处理的数据,没有则代表没有数据
            $data = $this->check_model->page('check', $checkWhere, $page_num, $start_position); //limit查询的数据


//            __format($data);

            if ($data[0]) {

                //根据关联id查询关联表单的数据
//                    $ids = array_column($data[0], 'relevanceId');
//                    foreach ($ids as $k=>$v){
//                        $arg = [
//                            'id' => $v
//                        ];
//                        $field = 'id,needCode,type,name,createTime,source,contactMan';
//                        $procurementData[] = $this->procurement_model->getByFieldQueryData($field,$arg,0);
//                    }


                foreach ($data[0] as $k => $v) {
                    if ($v['checkOrderType'] == '普通需求单' || $v['checkOrderType'] == '采购项目单' || $v['checkOrderType'] == '招投标项目需求单') {
                        $arg = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field = 'id,needCode,type,name,createTime,source,contactMan';
                        $procurementData[] = $this->procurement_model->getByFieldQueryData($field, $arg, 0);
                    }
                    if ($v['checkOrderType'] == '采购退货单' || $v['checkOrderType'] == '采购换货单') {
                        $arg2 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field2 = 'id,code,type,name,createTime,source';
                        $returnOrderData[] = $this->returnOrder_model->getByFieldQueryData($field2, $arg2, 0);
                    }
                    if ($v['checkOrderType'] == '采购进货单') {
                        $arg3 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field3 = 'id,purchaseCode,type,name,createTime,source,createMan';
                        $purchaseOrderData[] = $this->purchaseOrder_model->getByFieldQueryData($field3, $arg3, 0);
                    }
                    if ($v['checkOrderType'] == '商品出库单') {
                        $arg4 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field4 = 'id,stockOutCode,type,createTime,createMan,name,source,createMan';
                        $stockOutData[] = $this->stockOut_model->getByFieldQueryData($field4, $arg4, 0);
                    }
                    if ($v['checkOrderType'] == '商品入库单') {
                        $arg5 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field5 = 'id,stockPutCode,type,createTime,createMan,name,source,createMan';
                        $stockPutData[] = $this->stockPut_model->getByFieldQueryData($field5, $arg5, 0);
                    }
                }



//                    echo '<pre>';
//                    __format($data[0]);
//                    print_r($procurementData);
//                    print_r($returnOrderData);
//                    print_r($purchaseOrderData);
//                    print_r($stockOutData);
//                    print_r($stockPutData);
//                    die;

//                __format($stockOutData);

                //条件判断审核表中的checkOrderType和各类表的中的type一直，因为有时候会出现id相同的情况

                //这里判断  $变量名[0]  第一个key0的是因为,此时就选没有数据,走到最后也会生成一个数组,而数组的key就是0起始.
                if(isset($procurementData) && $procurementData[0]){
                    foreach ($data[0] as $k2 => $v2) {
                        foreach ($procurementData as $k3 => $v3 ) {
                            if (empty($v3)) {
                                unset($procurementData[$k3]);
                                unset($data[0][$k2]);
                                continue;
                            }

                            //todo 数组偏移量 Undefined offset: 0
                            if($v2['relevanceId'] == $v3['id'] && $v2['checkOrderType'] == $v3['type']){
                                @$procurementData[$k3]['checkId'] = $data[0][$k2]['id'];
                                @$procurementData[$k3]['checker'] = $data[0][$k2]['checker'];
                                @$procurementData[$k3]['status'] = $data[0][$k2]['status'];
                                @$procurementData[$k3]['checkStatus'] = $data[0][$k2]['checkStatus'];
                                break;

                            }


                        }
                    }
                    $a[] = $procurementData;
                }



                if(isset($returnOrderData) && $returnOrderData[0]){
                    foreach ($data[0] as $k5 => $v5) {
                        foreach ($returnOrderData as $k4 => $v4) {
                            if (empty($v4)) {
                                unset($returnOrderData[$k4]);
//                                unset($data[0][$k5]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
                            if($v5['relevanceId'] == $v4['id'] && $v5['checkOrderType'] == $v4['type']) {

                                @$returnOrderData[$k4]['checkId'] = $data[0][$k5]['id'];
                                @$returnOrderData[$k4]['checker'] = $data[0][$k5]['checker'];
                                @$returnOrderData[$k4]['status'] = $data[0][$k5]['status'];
                                @$returnOrderData[$k4]['checkStatus'] = $data[0][$k5]['checkStatus'];
                                break;
                            }
                        }
                    }
                    $a[] = $returnOrderData;

                }



                if(isset($purchaseOrderData) && $purchaseOrderData[0]){
                   foreach ($data[0] as $k6 => $v6) {
                       foreach ($purchaseOrderData as $k7 => $v7) {
                           if (empty($v7)) {
                               unset($purchaseOrderData[$k7]);
                               unset($data[0][$k6]);
                               continue;
                           }
                           //todo 数组偏移量 Undefined offset: 0
                           if($v6['relevanceId'] == $v7['id'] && $v6['checkOrderType'] == $v7['type']) {

                               @$purchaseOrderData[$k7]['checkId'] = $data[0][$k6]['id'];
                               @$purchaseOrderData[$k7]['checker'] = $data[0][$k6]['checker'];
                               @$purchaseOrderData[$k7]['status'] = $data[0][$k6]['status'];
                               @$purchaseOrderData[$k7]['checkStatus'] = $data[0][$k6]['checkStatus'];
                               break;

                           }
                       }
                   }

                   $a[] = $purchaseOrderData;
               }



//               __format($stockOutData);
//                __format($data[0]);

//                echo '<pre>';
//                print_r($stockOutData);
//                print_r($data[0]);
//                die;
                if(isset($stockOutData) && $stockOutData[0]){
                    foreach ($data[0] as $k8 => $v8) {
                        foreach ($stockOutData as $k9 => $v9) {
                            if (empty($v9)) {
                                unset($stockOutData[$k9]);
                                unset($data[0][$k8]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
//                            __logInfo('relevanceId',$v8['relevanceId']);
//                            __logInfo('id',$v9['id']);
//                            __logInfo('checkOrderType',$v8['checkOrderType']);
//                            __logInfo('type',$v9['type']);

                            if($v8['relevanceId'] == $v9['id'] && $v8['checkOrderType'] == $v9['type']) {

                                @$stockOutData[$k9]['checkId'] = $data[0][$k8]['id'];
                                @$stockOutData[$k9]['checker'] = $data[0][$k8]['checker'];
                                @$stockOutData[$k9]['status'] = $data[0][$k8]['status'];
                                @$stockOutData[$k9]['checkStatus'] = $data[0][$k8]['checkStatus'];
                                break;

                            }
                        }
                    }

                    $a[] = $stockOutData;
                }



                if(isset($stockPutData) && $stockPutData[0]){
                    foreach ($data[0] as $k10 => $v10) {
                        foreach ($stockPutData as $k11 => $v11) {
                            if (empty($v11)) {
                                unset($stockPutData[$k11]);
                                unset($data[0][$k10]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
                            if($v10['relevanceId'] == $v11['id'] && $v10['checkOrderType'] == $v11['type']) {

                                @$stockPutData[$k11]['checkId'] = $data[0][$k10]['id'];
                                @$stockPutData[$k11]['checker'] = $data[0][$k10]['checker'];
                                @$stockPutData[$k11]['status'] = $data[0][$k10]['status'];
                                @$stockPutData[$k11]['checkStatus'] = $data[0][$k10]['checkStatus'];
                                break;

                            }
                        }
                    }

                    $a[] = $stockPutData;
                }

//                __format($a);



//                    echo '<pre>';
//                    print_r($a);
//                    die;
                //二维数组变为一维数组
                if(isset($a) && $a){
                    foreach ($a as $k8 => $v8) {
                        foreach ($v8 as $k9 => $v9) {
                            $arr[] = $v9;
                        }
                    }
//                    __format($arr);

                    $viewData = [
                        'list' => $arr,  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum' => $page_num //每页显示多少条

                    ];

                    $this->load->view('admin/check/list', $viewData);
                }else{
                    $viewData = [
                        'list' => '',  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum' => $page_num //每页显示多少条
                    ];
                    $this->load->view('admin/check/list', $viewData);
                }
            } else {
                $viewData = [
                    'list' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];

                $this->load->view('admin/check/list', $viewData);

            }

        } elseif ($_GET['type'] == 2) {
            $checkWhere = [
                'type' => 2,
                //更换成动态名称
               'checker' =>  $_COOKIE['nickname'],
                'checkOrderType' => '招标项目评审'
                // 'checker' => '曾健健'
            ];

//                echo '<pre>';
            //查询审核的数据
            //刚进来为待处理的数据,没有则代表没有数据
            $data = $this->check_model->page('check', $checkWhere, $page_num, $start_position); //limit查询的数据
//                echo '<pre>';
//                print_r($data);
//                die();


//                echo '<pre>';
//                print_r($data);
//                die;

            //查询管理的标书审核id
            if($data[0]){
                foreach ($data[0] as $k=>$v){
                    $arg = [
                        'id' => $v['relevanceId']
                    ];
                    $field = 'id,code,name,createTime,needAmount,address';
                    $biddingCheck[] = $this->biddingcheck_model->getByFieldQueryData($field, $arg, 0);

//                    if ($v['checkOrderType'] == '招标项目评审') {
//                        $arg = [
//                            'id' => $v['relevanceId']
//                        ];
//                        $field = 'id,code,name,createTime,needAmount,address';
//                        $biddingCheck[] = $this->biddingcheck_model->getByFieldQueryData($field, $arg, 0);
//                    }


//                    if ($v['checkOrderType'] == '招标项目定标') {
//                        $arg2 = [
//                            'id' => $v['relevanceId']
//                        ];
//                        $field = 'id,code,name,createTime,needAmount,address';
//                        $confirmCheck[] = $this->biddingcheck_model->getByFieldQueryData($field, $arg2, 0);
//                    }

                }

            }

//                echo '<pre>';
//                print_r($biddingCheck);
//                die();


            foreach ($data[0] as $k2 => $v2) {
                if(isset($biddingCheck) && $biddingCheck){
                    foreach ($biddingCheck as $k3 => $v3) {
                        if ($v2['relevanceId'] == $v3['id']) {
                            $biddingCheck[$k3]['status'] = $v2['status'];
                            $biddingCheck[$k3]['checkId'] = $v2['id'];
                        }
                    }
                }

            }

//            if(isset($biddingCheck)){
//                $b[] = $biddingCheck;
//
//            }


//            foreach ($data[0] as  $v4) {
//                if(isset($confirmCheck) && $confirmCheck){
//                    foreach ($confirmCheck as $k5 => $v5) {
//                        if ($v4['relevanceId'] == $v5['id'] && !isset($v5['isType'])) {
//                            $confirmCheck[$k5]['status'] = $v4['status'];
//                            $confirmCheck[$k5]['checkId'] = $v4['id'];
//                            $confirmCheck[$k5]['isType'] = '招标项目定标';
//                        }
//                    }
//                }
//
//            }
//
//            if(isset($confirmCheck)){
//                $b[] = $confirmCheck;
//
//            }



//                echo '<pre>';
//                print_r($b);
//                die();

//            if(isset($b)){
//                foreach ($b as $k8 => $v8) {
//                    foreach ($v8 as $k9 => $v9) {
//                        $arr[] = $v9;
//                    }
//                }
//            }else{
//                $arr = [];
//            }


//                echo '<pre>';
//                print_r($biddingCheck);
//                die();

            if(isset($biddingCheck)){
                $data[0] = $biddingCheck;

            }else{
                $data[0] = [];
            }

//            echo '<pre>';
//                print_r($data);
//                die();
//

            if ($data[0]) {

                $viewData = [
                    'ztblist' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];
                $this->load->view('admin/check/list', $viewData);


            } else {

                $viewData = [
                    'ztblist' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];


                $this->load->view('admin/check/list', $viewData);

            }

        }else{
            //定标列表
            $checkWhere = [
                'type' => 2,
                //更换成动态名称
                'checker' =>  $_COOKIE['nickname'],
                'checkOrderType' => '招标项目定标'
                // 'checker' => '曾健健'
            ];

//                echo '<pre>';
            //查询审核的数据
            //刚进来为待处理的数据,没有则代表没有数据
            $data = $this->check_model->page('check', $checkWhere, $page_num, $start_position); //limit查询的数据
//                echo '<pre>';
//                print_r($data);
//                die();


//                echo '<pre>';
//                print_r($data);
//                die;

            //查询管理的标书审核id
            if($data[0]){
                foreach ($data[0] as $k=>$v){

                    $arg2 = [
//                        这个关联的是biddingcheck表的id
                        'relevanceId' => $v['relevanceId']
                    ];
                    $field = '*';
                    $confirmCheck[] = $this->biddingconfirm_model->getByFieldQueryData($field, $arg2, 0);


                }

            }

//                echo '<pre>';
//                print_r($data[0]);
//                print_r($confirmCheck);
//                die();


            foreach ($data[0] as  $v4) {
                if(isset($confirmCheck) && $confirmCheck){
                    foreach ($confirmCheck as $k5 => $v5) {
                        if ($v4['relevanceId'] == $v5['relevanceId']) {
                            $confirmCheck[$k5]['status'] = $v4['status'];
                            $confirmCheck[$k5]['checkId'] = $v4['id'];
                            $confirmCheck[$k5]['isType'] = '招标项目定标';
                        }
                    }
                }

            }

            /*

                    [id] => 35     biddingconfirm的id
                    [relevanceId] => 24   关联的biddingcheck的id
                    [status] => 0
                    [isEnd] => 0
                    [confirm] =>
                    [code] => ZBXM20170912949
                    [name] => 浙江省校内实验室3D打印机采购
                    [createTime] => 1505145600
                    [needAmount] => 1
                    [address] => 浙江省/杭州市/临安市
                    [checkId] => 135        checkId  check表的id
                    [isType] => 招标项目定标
            */

            if(isset($confirmCheck)){
                $data[0] = $confirmCheck;
            }else{
                $data[0] = [];
            }

//                echo '<pre>';
//                print_r($data);
//                die();
//

            if ($data[0]) {

                $viewData = [
                    'ztblist' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];
                $this->load->view('admin/check/list', $viewData);


            } else {

                $viewData = [
                    'ztblist' => '',  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum' => $page_num //每页显示多少条
                ];


                $this->load->view('admin/check/list', $viewData);

            }



        }


        }


    /**
     * 审核编辑
     */
    public function edit()
    {

        $data = [];

        $where = [
            //这个proId，就是个个单据的表，主键id
            'id' => $_GET['proId']
        ];
        $where2 = [
            'id' => $_GET['id']
        ];

//        $data['content'] = $_GET['content'];

        //查询当前需求,审批人列表

        //判断如果是退换货单，另外查询
        if($_GET['type'] == '采购退货单' || $_GET['type'] == '采购换货单'){
            $people = $this->returnOrder_model->getByFieldQueryData('*', $where, 0);

        }elseif($_GET['type'] == '普通需求单' || $_GET['type'] == '采购项目单' || $_GET['type'] == '招投标项目需求单'){
            $people = $this->procurement_model->getByFieldQueryData('*', $where, 0);

        }elseif($_GET['type'] == '商品出库单'){
            $people = $this->stockOut_model->getByFieldQueryData('*', $where, 0);
        }elseif($_GET['type'] == '商品入库单'){
            $people = $this->stockPut_model->getByFieldQueryData('*', $where, 0);
        }


//        echo '<pre>';
//        print_r($people);
//        die;

        //查询当前审核表的审核信息
        $info = $this->check_model->getByFieldQueryData('id,checkInfo,howLevelCheck', $where2, 0);

        $first[] = [
            'checker' => $_COOKIE['nickname'],
            'status' => $_GET['status'],
            'content' => $_GET['content']
        ];

        //判断是否第一次审核,为空表示第一次
        if ($info['checkInfo']) {
            $resul = json_decode($info['checkInfo'], true);

            $two = [
                'checker' => $_COOKIE['nickname'],
                'status' => $_GET['status'],
                'content' => $_GET['content']
            ];

            $resul[] = $two;

            $a = json_encode($resul, JSON_UNESCAPED_UNICODE);
        } else {
            $a = json_encode($first, JSON_UNESCAPED_UNICODE);
        }

//        print_r($a);
//        die;
        __logInfo('checkStatus',$_GET['checkStatus']);

        //判断当前审核状态码,为0表示第一次,之后每次取的相对应的审核人
        switch ($_GET['checkStatus'] + 1) {
            case 0:
                $checker = $people['director'];
                break;
            case 1:
                $checker = $people['president'];

                break;
            case 2:
                $checker = $people['chairman'];
                break;

            case 3:
                $checker = $people['financial'];
                break;
        }

        __logInfo('审核',$checker);


        //如果审核成功
        if ($_GET['status'] == 1) {
            $high = $_GET['checkStatus'] + 1;
            if ($high == $info['howLevelCheck']) {
                //更新需求/项目的状态
                $checkData = [
                    'status' => 2
                ];

                $data['status'] = 1;
                $data['checker'] = $_COOKIE['nickname'];


                //当所有审批完成之后加入到财务审核数据库中，

                //不同单据的变量名
                //这是普通需求单，项目单，招投标单
                if(isset($people['needCode'])){
                    $code = $people['needCode'];
                }
                //这是退换货单
                if(isset($people['code'])){
                    $code = $people['code'];
                }
                //这是商品入库单
                if(isset($people['stockPutCode'])){
                    $code = $people['stockPutCode'];
                }

                //这是商品出库单
                if(isset($people['stockOutCode'])){
                    $code = $people['stockOutCode'];
                }
                $goldCheck = [
                    'code' => $code, //
                    'type' => $people['type'],//单据类型
                    'name' => $people['name'],//单据类型
                    'createTime' => $people['createTime'],//单据类型
                    'source' => $people['source'],//单据类型
                    'attribute' => $people['attribute'],//单据类型
                    'status' => 1,//等于1表示已确定
                    'financial' => $people['financial'], //抄送人
                    'relevanceId' => $_GET['proId'] //关联的单据id
                ];
                $this->goldCheck_model->add($goldCheck);


                //审核商品出库单
                $checkData3 = [
                    'status' => 0
                ];
                $this->stockOut_model->edit($checkData3, $where);

                $this->stockPut_model->edit($checkData3, $where);

            } else {
                $checkData = [
                    'status' => 1
                ];
                $data['status'] = 0;

            }
            $this->procurement_model->edit($checkData, $where);
            $this->returnOrder_model->edit($checkData, $where);




            //审核完成插入数据库中
            if ($_GET['status'] == 1) {
                $data['checkStatus'] = $_GET['checkStatus'] + 1;
                $data['checker'] = $checker;
                $data['checkInfo'] = $a;
            }

            __logInfo('checker',$checker);


            $this->check_model->edit($data, $where2);
        } else {
            //审核失败的情况下
            $checkErrorData = [
                'status' => 4
            ];

            $this->procurement_model->edit($checkErrorData, $where);


            $errorData = [];
            $errorData['status'] = 2;
            $errorData['checkStatus'] = $_GET['checkStatus'] + 1;
            $errorData['checker'] = $checker;
            $errorData['checkInfo'] = $a;

            $this->check_model->edit($errorData, $where2);
        }


        //更新需求/项目的状态
//        $high = $_GET['checkStatus'] + 1;
//        if($high == $info['howLevelCheck']){
//            $checkData = [
//                'status' => 2
//            ];
//        }else{
//            $checkData = [
//                'status' => 1
//            ];
//        }
//        $this->procurement_model->edit($checkData,$where);


        redirect('admin/check/index?page=1&type=1');
    }

    /**
     * 选择不同状态,获取数据
     * 已审核为1,待审核为0
     */
    public function getStatus()
    {

        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if ($_POST['status']) {
            $checkWhere = [
                'status !=' => 0,
                //todo 如果需要显示自己审核通过的数据,则加上这个条件
//                'checker' => '李总'

            ];
        } else {
            $checkWhere = [
                'status' => 0,
                //更换成动态名称

                'checker' => $_COOKIE['nickname']
            ];
        }

        $data = $this->check_model->page('check', $checkWhere, $page_num, $start_position); //limit查询的数据

//                print_r($data[0]);
//                die();
        //如果有数据的情况下
        if ($data[0]) {
            $ids = array_column($data[0], 'relevanceId');
            foreach ($ids as $k => $v) {
                $arg = [
                    'id' => $v
                ];
                $field = 'id,needCode,type,name,createTime,source,contactMan';
                $procurementData[] = $this->procurement_model->getByFieldQueryData($field, $arg, 0);
            }

            foreach ($data[0] as $k2 => $v2) {
                foreach ($procurementData as $k3 => $v3) {
                    $procurementData[$k3]['checkId'] = $data[0][$k3]['id'];
                    $procurementData[$k3]['checker'] = $data[0][$k3]['checker'];
                    $procurementData[$k3]['status'] = $data[0][$k3]['status'];
                    $procurementData[$k3]['checkStatus'] = $data[0][$k3]['checkStatus'];

                }
            }

//                    print_r($procurementData);
//                    die;

            $viewData = [
                'list' => $procurementData,  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条

            ];

//            print_r($viewData);
//            die();

            echo json_encode($viewData, JSON_UNESCAPED_UNICODE);
//            $this->load->view('admin/check/list',$viewData);

        } else {
            $viewData = [
                'list' => '',  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum' => $page_num //每页显示多少条
            ];

            echo json_encode($viewData);

//            $this->load->view('admin/check/list',$viewData);

        }

    }


    public function detail()
    {
        //根据传过来的id,查询需求单/项目单 数据页面


        $type = $_GET['type'];  //项目单类型


        if ($type == '普通需求单' || $type== '采购项目单' || $type == '招投标项目需求单') {
            $arg = [
                'id' => $_GET['id']
            ];
            $field = '*';
            $data['list']  = $this->procurement_model->getByFieldQueryData($field, $arg, 0);
        }
        if ($type == '采购退货单' || $type == '采购换货单') {
            $arg2 = [
                'id' => $_GET['id']
            ];
            $field2 = '*';
            $data['list']  = $this->returnOrder_model->getByFieldQueryData($field2, $arg2, 0);
        }
        if ($type == '采购进货单') {
            $arg3 = [
                'id' => $_GET['id']
            ];
            $field3 = '*';
            $data['list']  = $this->purchaseOrder_model->getByFieldQueryData($field3, $arg3, 0);
        }
        if ($type == '商品出库单') {
            $arg3 = [
                'id' => $_GET['id']
            ];
            $field3 = '*';
            $data['list']  = $this->stockOut_model->getByFieldQueryData($field3, $arg3, 0);
        }
        if ($type == '商品入库单') {
            $arg4 = [
                'id' => $_GET['id']
            ];
            $field3 = '*';
            $data['list']  = $this->stockPut_model->getByFieldQueryData($field3, $arg4, 0);
        }

//        __format($data);
//        $data['list'] = json_encode($data['list']['goodsMessage'],true);

//        echo '<pre>';
//        print_r($data);
//        die;

        //如果不存在直接跳转到404页面
        if (!$data['list']) {
            redirect('/home/error');
        }

        $arg4 = [
            'id' => $_GET['checkId'] //审核库中的对应id
        ];

        $checkInfo  = $this->check_model->getByFieldQueryData('id,checkInfo', $arg4, 0);

        //如果审核信息为真就显示
        if($checkInfo){
            $data['list']['checkInfo'] = json_decode($checkInfo['checkInfo'],true);
        }

//        print_r($data['list']['checkInfo']);
//        die;

        //判断审核信息里面，是否有当前用户，如果有表示已审批，只显示，没有表示未审核，显示出来审核按钮
        if($data['list']['checkInfo']){
            foreach ($data['list']['checkInfo'] as $k=>$v){
                if($_COOKIE['nickname'] == $v['checker']){
                    $data['list']['isCheck'] = 1;
                }else{
                    $data['list']['isCheck'] = 0;
                }
            }
        }else{
            $data['list']['isCheck'] = 0;
        }


//         echo '<pre>';
//        print_r($data['list']['checkInfo']);
//        die;

        //如果不存在直接跳转到404页面
        if (!$data['list']) {
            redirect('/home/error');
        }

//        echo '<pre>';
//        print_r($data);
//        echo $_COOKIE['nickname'];
//        die;


        //进货单另外读取
        $this->load->view('/admin/check/detail', $data);

    }


}